<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>生成平衡数组的方案数次数</title>
</head>
<body>
  <script>
    function fn(nums) {
      let odd1 = 0, even1 = 0;
      let odd2 = 0, even2 = 0;
      for (let i = 0; i < nums.length; ++i) {
        if ((i & 1) !== 0) {
          odd2 += nums[i]
        } else {
          even2 += nums[i]
        }
      }
      let res = 0
      for (let i = 0; i < nums.length; ++i) {
        if ((i & 1) != 0) {
          odd2 -= nums[i]
        } else {
          even2 -= nums[i]
        }
        if (odd1 + even2 === odd2 + even1) {
          ++res;
        }
        if ((i & 1) !== 0) {
          odd1 += nums[i]
        } else {
          even1 += nums[i]
        }
      }
      return res
    }
    console.log(fn([2, 1, 6, 4]))
  </script>
</body>
</html>